package com.glory.dgcpcpda.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.entity.EdcSetBinEntity;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.MLotListEntity;

import java.util.List;

public class WaferInListAdapter extends BaseQuickAdapter<MLotListEntity, BaseViewHolder> {


    public WaferInListAdapter(@Nullable List<MLotListEntity> data) {
        super(R.layout.adapter_wafer_in,data);

    }

    @Override
    protected void convert(BaseViewHolder helper, MLotListEntity item) {
        String desc="";
        if(item.getSTATE().equals("OUT")){
            desc="已下架";
        }else{
            desc=item.getTRANSSTORAGEID();
        }
       helper.setText(R.id.tvMlotId,String.valueOf(item.getPARTNERLOTID()))
                .setText(R.id.tvWaferName,item.getMATERIALDESC())
                .setText(R.id.etSubQty,item.getSUBQTY())
                .setText(R.id.tvLocation,desc)
               .setText(R.id.tvCustName,item.getPOSITION())
                .setText(R.id.tvTime,item.getRESERVED1());

        EditText etSubQty = helper.getView(R.id.etSubQty);

        setEditListener(etSubQty,item);

    }

    private void setEditListener(EditText editText, MLotListEntity item) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.toString().equals("")){
                    item.setSUBQTY("0");
                }else if (!s.toString().equals("0")&&s.toString().startsWith("0")){
                    String sub=s.toString().substring(1);
                    item.setSUBQTY(sub);
                }else {
                    item.setSUBQTY(s.toString());
                }
            }
        };

        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }

}
